package org.jivesoftware.smackx.workgroup.ext.history;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;

public class AgentChatHistory extends IQ
{
  public static final String ELEMENT_NAME = "chat-sessions";
  public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
  private List agentChatSessions = new ArrayList();
  private String agentJID;
  private int maxSessions;
  private long startDate;

  public AgentChatHistory(String paramString, int paramInt)
  {
    this.agentJID = paramString;
    this.maxSessions = paramInt;
    this.startDate = 0L;
  }

  public AgentChatHistory(String paramString, int paramInt, Date paramDate)
  {
    this.agentJID = paramString;
    this.maxSessions = paramInt;
    this.startDate = paramDate.getTime();
  }

  public void addChatSession(AgentChatSession paramAgentChatSession)
  {
    this.agentChatSessions.add(paramAgentChatSession);
  }

  public Collection getAgentChatSessions()
  {
    return this.agentChatSessions;
  }

  public String getChildElementXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<").append("chat-sessions").append(" xmlns=");
    localStringBuilder.append('"');
    localStringBuilder.append("http://jivesoftware.com/protocol/workgroup");
    localStringBuilder.append('"');
    localStringBuilder.append(" agentJID=\"" + this.agentJID + "\"");
    localStringBuilder.append(" maxSessions=\"" + this.maxSessions + "\"");
    localStringBuilder.append(" startDate=\"" + this.startDate + "\"");
    localStringBuilder.append("></").append("chat-sessions").append("> ");
    return localStringBuilder.toString();
  }

  public static class InternalProvider
  implements IQProvider
  {
    private AgentChatSession parseChatSetting(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      int i = 0;
      Date localDate = null;
      long l = 0L;
      String str1 = null;
      String str2 = null;
      String str3 = null;
      String str4 = null;
      while (true)
      {
        int j;
        do
          while (true)
          {
            while (true)
            {
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    while (true)
                    {
                      if (i != 0)
                        return new AgentChatSession(localDate, l, str1, str2, str3, str4);
                      j = paramXmlPullParser.next();
                      if ((j != 2) || (!("date".equals(paramXmlPullParser.getName()))))
                        break;
                      localDate = new Date(Long.valueOf(paramXmlPullParser.nextText()).longValue());
                    }
                    if ((j != 2) || (!("duration".equals(paramXmlPullParser.getName()))))
                      break;
                    l = Long.valueOf(paramXmlPullParser.nextText()).longValue();
                  }
                  if ((j != 2) || (!("visitorsName".equals(paramXmlPullParser.getName()))))
                    break;
                  str1 = paramXmlPullParser.nextText();
                }
                if ((j != 2) || (!("visitorsEmail".equals(paramXmlPullParser.getName()))))
                  break;
                str2 = paramXmlPullParser.nextText();
              }
              if ((j != 2) || (!("sessionID".equals(paramXmlPullParser.getName()))))
                break;
              str3 = paramXmlPullParser.nextText();
            }
            if ((j != 2) || (!("question".equals(paramXmlPullParser.getName()))))
              break;
            str4 = paramXmlPullParser.nextText();
          }
        while ((j != 3) || (!("chat-session".equals(paramXmlPullParser.getName()))));
        i = 1;
      }
    }

    public IQ parseIQ(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      if (paramXmlPullParser.getEventType() != 2)
        throw new IllegalStateException("Parser not in proper position, or bad XML.");
      AgentChatHistory localAgentChatHistory = new AgentChatHistory();
      int i = 0;
      while (true)
      {
        int j;
        do
          while (true)
          {
            if (i != 0)
              return localAgentChatHistory;
            j = paramXmlPullParser.next();
            if ((j != 2) || (!("chat-session".equals(paramXmlPullParser.getName()))))
              break;
            localAgentChatHistory.addChatSession(parseChatSetting(paramXmlPullParser));
          }
        while ((j != 3) || (!("chat-sessions".equals(paramXmlPullParser.getName()))));
        i = 1;
      }
    }
  }
}